package com.zhenmyan.secdeal.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * description:
 *
 * rabbitmq都是现将消息发送给交换机，然后转发到队列中
 * 而转发有不同的方式，这里是topic模式
 *
 * 配置类：需要设置队列和交换机名称，然后将队列与路由key进行绑定，与direct不同的是这里路由key可以是通配符模式
 *
 * @author 颜真明
 * @date 2023/5/15  9:57
 */
@Configuration
public class RabbitMQTopicConfig {

    private static final String TOPIC_QUEUE_1= "topic_queue_1";

    private static final String TOPIC_QUEUE_2 = "topic_queue_2";

    private static final String TOPIC_EXCHANGE = "topic_exchange";

    // topic模式中 * 匹配一个精确的单词
    private static final String ROUTING_KEY_1 = "#.queue.#";

    // topic模式中 # 匹配零个或多个单词
    private static final String ROUTING_KEY_2 = "*.queue.#";

    @Bean
    public Queue topicQueue1(){
        return new Queue(TOPIC_QUEUE_1);
    }
    @Bean
    public Queue topicQueue2(){
        return new Queue(TOPIC_QUEUE_2);
    }

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(TOPIC_EXCHANGE);
    }

    /**
     * 将队列1绑定到路由key1
     *
     * @return
     */
    @Bean
    public Binding topicBinding1() {
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with(ROUTING_KEY_1);
    }

    /**
     * 将队列2绑定到路由key2
     *
     * @return
     */
    @Bean
    public Binding topicBinding2() {
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with(ROUTING_KEY_2);
    }

}
